package Enrollment;

import java.util.*;
import java.util.stream.Collectors;

public class StreamFinalExercise {
    public static void main(String[] args) {
        List<Enrollment> enrollments = Arrays.asList(
            new Enrollment("张三", "数学", 85, true),
            new Enrollment("李四", "数学", 92, true),
            new Enrollment("王五", "数学", 78, false),
            new Enrollment("赵六", "英语", 88, true),
            new Enrollment("孙七", "英语", 90, true),
            new Enrollment("周八", "英语", 60, false),
            new Enrollment("吴九", "物理", 75, true),
            new Enrollment("郑十", "物理", 80, true)
        );

        // 1️⃣ 找出每门课程中成绩最高的报名记录
        Map<String, Enrollment> topStudentsByCourse = enrollments.stream().collect(Collectors.groupingBy(
                Enrollment::getCourseName,
                Collectors.maxBy(Comparator.comparingInt(Enrollment::getScore))
        )).entrySet().stream()
            .collect(Collectors.toMap(
                Map.Entry::getKey,
                entry -> entry.getValue().get()
            ));

        // 2️⃣ 统计每门课程中通过考试的人数
        Map<String, Long> passedCountByCourse = enrollments.stream().filter(Enrollment::isPassed).collect(Collectors.groupingBy(Enrollment::getCourseName,Collectors.counting()));
        // 3️⃣ 按通过人数从高到低排序并输出
        List<Map.Entry<String, Long>> sortedCourses = new ArrayList<>(passedCountByCourse.entrySet());
        // 排序逻辑在这里
        sortedCourses.sort((e1, e2) -> Long.compare(e2.getValue(), e1.getValue()));

        System.out.println("每门课程最高分记录：" + topStudentsByCourse);
        System.out.println("每门课程通过人数：" + passedCountByCourse);
        System.out.println("按通过人数排序的课程：");
        sortedCourses.forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
    }
}